package com.medmarkmobile.connection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.medmarkmobile.gui.Perfil_Fragment;

import android.app.ProgressDialog;
import android.os.AsyncTask;

public class UpdateProfile extends AsyncTask<String, Boolean, Boolean>{
	
	private ProgressDialog progressDialog;
	Perfil_Fragment myActivity;
	
	public UpdateProfile(Perfil_Fragment myActivity){
		progressDialog = new ProgressDialog(myActivity.getActivity());
		this.myActivity = myActivity;
	}
	
	@Override
	protected Boolean doInBackground(String... params) {
Connection conn = null;
		
		try{
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			String url = "jdbc:mysql://54.94.138.7:3306/medmark";
			conn = DriverManager.getConnection(url, "adminmedmark", "adminmedmark123");
			
			String query = "update tb_pacientes set nome = '" + params[0] + "' , idade = " + params[1] + 
				" , cpf = " + params[2] + " , rua = '" + params[3] + "' , bairro = '" +params[4]  +
				"' , cidade = '" + params[5] + "' , estado = '" + params[6] + "' , complemento = '" + params[7] +
				"' , numero = " + params[8] + ", telefone = " + params[9] + ", sexo = '" + params[10] + 
				"' , estado_civil = '" + params[11] + "' , profissao = '" + params[12] + "' , senha = '" + params[13] + 
				"' , login = '" + params[14] + "' where codigo = " + params[15] ;
			
			
			PreparedStatement preparedStmt = conn.prepareStatement(query);
		  
		      
		    preparedStmt.executeUpdate();
		      
		    conn.close();
			
		}
		catch(SQLException e){
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return true;
	}
	
	@Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog.setCancelable(true);
        progressDialog.setMessage("Loading...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setProgress(0);
        progressDialog.show();
    }
	
	@Override
	protected void onPostExecute(Boolean result){
		progressDialog.dismiss();
		myActivity.removeBotaoCancelar();
		
	}

}
